/**
 * This file is released under the GNU General Public License.
 * Refer to the COPYING file distributed with this package.
 *
 * Copyright (c) 2008-2009 WURFL-Pro srl
 */
package net.sourceforge.wurfl.core.handlers.matchers;

import java.util.SortedSet;

import net.sourceforge.wurfl.core.handlers.Handler;
import net.sourceforge.wurfl.core.utils.StringUtils;

public class MSIEMatcher extends AbstractMatcher {
	
	public MSIEMatcher(Handler handler) {
		super(handler);
	}
	
	/**
	 * Apply RIS(secondSemiColumn)
	 */
	protected String lookForMatchingUserAgent(SortedSet userAgentsSet, String userAgent) {
		
		int tolerance = secondSemiColon(userAgent);
		
		if (logger.isDebugEnabled()) {
			logger.debug("Appling RIS(" + tolerance + ") UA: " + userAgent);
		}
		
		String match = StringUtils.risMatch(userAgentsSet, userAgent, tolerance);
		
		return match;
	}
	
	/**
	 * Return the index of second semi-column of the given string.
	 * 
	 * @param target
	 *            A string on which find second slash.
	 * @return A index of second slash in the string.
	 */
	private static int secondSemiColon(String target) {
		
		return StringUtils.ordinalIndexOfOrLength(target, ";", 2); 
	}

}
